package nccloud.web.hrppl.pjapplication.pjappl.action;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import nc.itf.hrppl.pjapplication.pjappl.IPJApplHVOService;
import nc.itf.uif.pub.IUifService;
import nc.mddb.baseutil.MDDAOUtil;
import nc.vo.bd.defdoc.DefdocVO;
import nc.vo.hrppl.pjapplication.AggPjapplVO;
import nccloud.framework.service.ServiceLocator;
import nccloud.framework.web.container.IRequest;
import nccloud.framework.web.ui.model.row.Cell;
import nccloud.framework.web.ui.model.row.Row;
import nccloud.framework.web.ui.pattern.extbillcard.ExtBillCard;
import nccloud.web.codeplatform.framework.action.base.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 退库Action
 */
public class UnWarehousingAction extends BaseAction{
	@Override
	public Object doAction(IRequest request, RequstParamWapper paramWapper) throws Throwable{
		//json数据转换
		RequestDTO param = VOTransform.toVO(paramWapper.requestString,RequestDTO.class);
		//获取参数
//		String pk = param.getPk();
		String[] pks = param.getPk().split(",");
		JSONObject json = JSON.parseObject(paramWapper.requestString);
		String bz = json.getString("bz");

		IPJApplHVOService service = ServiceLocator.find(IPJApplHVOService.class);

		if (pks.length == 1)
		{
//			AggPjapplVO vo = service.unwarehousing(pks[0]); 增加备注改动
			AggPjapplVO vo = service.unwarehousing(pks[0], bz);
			if(vo == null){
				return null;
			}
			ResultWapper o = (ResultWapper) buildResult(param,true,null,vo);
			ExtBillCard data = (ExtBillCard) o.getData();
			Row row = data.getHead().getModel().getRows()[0];
			Cell expertgroupmember = row.getValues().get("expertgroupmember");
			if (expertgroupmember != null && expertgroupmember.getValue() != null){
				String value = expertgroupmember.getValue().toString();
				if (value.contains(",")){
					IUifService uifService = ServiceLocator.find(IUifService.class);
					DefdocVO[] defdocVOS = (DefdocVO[]) uifService.queryByCondition(DefdocVO.class, " name in " + MDDAOUtil.getInSql(value.split(",")) + " or pk_defdoc in " + MDDAOUtil.getInSql(value.split(",")));
					if (defdocVOS != null && defdocVOS.length > 0){
						StringBuffer sb_display = new StringBuffer();
						StringBuffer sb_value = new StringBuffer();
						for (DefdocVO defdocVO : defdocVOS) {
							sb_display.append(defdocVO.getName()).append(",");
							sb_value.append(defdocVO.getPk_defdoc()).append(",");
						}
						expertgroupmember.setDisplay(sb_display.deleteCharAt(sb_display.length() - 1).toString());
						expertgroupmember.setValue(sb_value.deleteCharAt(sb_value.length() - 1).toString());
					}
				}
			}
			return o;
		} else {
			Map<String, Object> userInfo = new HashMap<>();
			for(String pk:pks)
			{
//				AggPjapplVO vo = service.unwarehousing(pk); 增加备注，改动
				AggPjapplVO vo = service.unwarehousing(pk, bz);
				if(vo == null){
					return null;
				}
			}
			userInfo.put("data", true);
			return userInfo;
		}
		

	}

}